home *** CD-ROM | disk | FTP | other *** search
/ MacTech 1 to 12 / MacTech-vol-1-12.toast / Source / MacTech® Magazine / Volume 07 - 1991 / 07.05 May 91 / Multifile source ƒ / mfdemo.c < prev    next >
Encoding:
C/C++ Source or Header  |  1987-12-29  |  4.8 KB  |  246 lines  |  [TEXT/MPS ]

  1. /*
  2. ** File mfdemo.c
  3. ** Source file for demonstration of
  4. ** MultiFile Select dialog
  5. **
  6. ** Copyright © Eric Schlegel 1987, 1988
  7. */
  8.  
  9.  
  10. #include    <types.h>
  11. #include    <quickdraw.h>
  12. #include    <fonts.h>
  13. #include    <events.h>
  14. #include    <windows.h>
  15. #include    <menus.h>
  16. #include    <textedit.h>
  17. #include    <dialogs.h>
  18. #include    <packages.h>
  19. #include    <memory.h>
  20. #include    <segload.h>
  21. #include    <files.h>
  22. #include    "mfile.h"
  23.  
  24. #define AppParmHandle    (*((long *)(0xaec)))
  25.  
  26. typedef struct parmhead {
  27.     short    msg;
  28.     short    count;
  29. } PARMHEAD;
  30.  
  31. /* launch struct. See TN126    */
  32. typedef struct lstruct {
  33.     StringPtr    pfName;
  34.     short        param;
  35.     short        LC;
  36.     long        extBlockLen;
  37.     short        fFlags;
  38.     long        launchFlags;
  39. } LSTRUCT;
  40.  
  41. pascal short dolaunch(lsp)
  42.     LSTRUCT        *lsp;
  43.     extern;
  44.     
  45. /* SFReply rec for SFGetFile for app    */
  46. SFReply        appreply;
  47. LSTRUCT        ls;
  48.  
  49. main()
  50. {
  51.     Boolean            getapp();
  52.     void            getdocs();
  53.     void            launchit();
  54.     
  55.     EventRecord        ev;
  56.     int                ch;
  57.     
  58.     InitGraf(&qd.thePort);
  59.     InitFonts();
  60.     InitWindows();
  61.     InitMenus();
  62.     TEInit();
  63.     InitDialogs(NULL);
  64.     
  65.     if (!getapp())
  66.         ExitToShell();
  67.         
  68.     getdocs();
  69.     
  70.     launchit();
  71.     
  72.     /* we get here if under MultiFinder    */
  73.     /* wait for a cmd-Q                    */
  74.     while (1)
  75.         if (GetNextEvent(everyEvent, &ev))
  76.             if ((ev.what == keyDown) || (ev.what == autoKey)) {
  77.                 ch = ev.message & charCodeMask;
  78.                 if (((ch == 'q') || (ch == 'Q')) &&
  79.                     (ev.modifiers & cmdKey))
  80.                     break;
  81.             }
  82.             
  83.     ExitToShell();
  84. }
  85.  
  86. /* use SFGetFile to get an application to    */
  87. /* launch. returns true if user clicks OK    */
  88. /* and false if user clicks Cancel            */
  89. Boolean getapp()
  90. {
  91.     Point            where;
  92.     SFTypeList        types;
  93.     
  94.     SetPt(&where, 50, 50);
  95.     types[0] = 'APPL';
  96.     SFGetFile(&where, "", NULL, 1, types, NULL, &appreply);
  97.     return(appreply.good);
  98. }
  99.  
  100. /* get the documents with multifile()    */
  101. void getdocs()
  102. {
  103.     pascal Boolean    docfilt();
  104.     void            mkdoclist();
  105.     
  106.     Point            where;
  107.     SFTypeList        types;
  108.     
  109.     get_tl(&where);
  110.     if (multifile(&where, "Select documents:", docfilt, -1, types))
  111.         mkdoclist();
  112. }
  113.  
  114. /* filefilter function for selecting documents    */
  115. pascal Boolean docfilt(pb)
  116. ParmBlkPtr    pb;
  117. {
  118.     /* always show folders; also show noninvisible documents    */
  119.     if (pb->fileParam.ioFlAttrib & 0x10)
  120.         return(true);
  121.     else
  122.         return((pb->fileParam.ioFlFndrInfo.fdFlags & fInvisible) ||
  123.             (pb->fileParam.ioFlFndrInfo.fdType == 'APPL'));
  124. }
  125.  
  126. /* set up the application parameters    */
  127. void mkdoclist()
  128. {
  129.     void        newlist();
  130.     void        addfile();
  131.     
  132.     int            nfiles;
  133.     int            i;
  134.     FSTRUCT        fstrct;
  135.     
  136.     newlist();
  137.     
  138.     nfiles = numfiles();
  139.     for (i = 1; i <= nfiles; i++) {
  140.         getfile(i, &fstrct);
  141.         addfile(&fstrct);
  142.     }
  143. }
  144.  
  145. /* creates a new application parameters list    */
  146. void newlist()
  147. {
  148.     Handle    parms;
  149.     THz        oldzone;
  150.     
  151.     /* if AppParmHandle already has a valid handle,    */
  152.     /* just resize it to the starting size.            */
  153.     if (parms = (Handle)AppParmHandle) {
  154.         HUnlock(parms);
  155.         SetHandleSize(parms, 4);
  156.         return;
  157.     }
  158.     
  159.     /* allocate memory in sysheap so it    */
  160.     /* stays around after we quit        */
  161.     oldzone = GetZone();
  162.     SetZone(SystemZone());
  163.     
  164.     /* make sure we get the memory    */
  165.     if (parms = NewHandle(4)) {
  166.         ((PARMHEAD *)(*parms))->msg = appOpen;
  167.         ((PARMHEAD *)(*parms))->count = 0;
  168.     }
  169.     
  170.     SetZone(oldzone);
  171.     
  172.     AppParmHandle = (long)parms;
  173. }
  174.  
  175. /* adds file described by fstrct to app params list    */
  176. void addfile(fstrct)
  177. FSTRUCT        *fstrct;
  178. {
  179.     Handle        parms;
  180.     long        oldsize;
  181.     int            fsize;
  182.     char        *aptr;
  183.     
  184.     /* return if parms is null    */
  185.     if (!(parms = (Handle)AppParmHandle))
  186.         return;
  187.     
  188.     /* get length of filename; if length of    fname    */
  189.     /* plus length byte is odd, make it even        */
  190.     fsize = fstrct->fname.length;
  191.     fsize++;
  192.     if (fsize & 0x01)
  193.         fsize++;
  194.         
  195.     /* grow parms to hold new file.    */
  196.     /* 8 bytes for vrefnum, type,    */
  197.     /* and version.                    */
  198.     oldsize = GetHandleSize(parms);
  199.     SetHandleSize(parms, oldsize + 8 + fsize);
  200.     
  201.     /* return if not enough mem    */
  202.     if (MemError())
  203.         return;    
  204.     
  205.     /* stuff in the file info    */
  206.     aptr = (char *)((long)*parms + oldsize);
  207.     *((short *)aptr)++ = fstrct->vrefnum;
  208.     *((long *)aptr)++ = fstrct->ftype;
  209.     *((short *)aptr)++ = fstrct->fver;
  210.     BlockMove(&fstrct->fname, aptr, fsize);
  211.     
  212.     /* increment file count    */
  213.     ((PARMHEAD *)(*parms))->count++;
  214. }
  215.  
  216. /* launches application described by appreply    */
  217. void launchit()
  218. {
  219.     char            fname[64];
  220.     HParamBlockRec    hpb;
  221.     
  222.     /* copy app name to fname. SFReply.fName is a Str63    */
  223.     BlockMove(&appreply.fName, fname, 64);
  224.     
  225.     hpb.fileParam.ioCompletion = 0L;
  226.     hpb.fileParam.ioNamePtr = fname;
  227.     hpb.fileParam.ioVRefNum = appreply.vRefNum;
  228.     hpb.fileParam.ioFDirIndex = 0;
  229.     hpb.fileParam.ioDirID = 0L;
  230.     PBHGetFInfo(&hpb, false);
  231.     
  232.     /* set current volume to app's directory    */
  233.     SetVol(NULL, appreply.vRefNum);
  234.     
  235.     /* set up launch struct. param=0 indicates    */
  236.     /* no alternate screen or sound buffers        */
  237.     ls.pfName = fname;
  238.     ls.param = 0;
  239.     ls.LC = 'LC';
  240.     ls.extBlockLen = 6;
  241.     ls.fFlags = hpb.fileParam.ioFlFndrInfo.fdFlags;
  242.     ls.launchFlags = 0x40000000;
  243.     
  244.     /* ignore the error code after launch    */
  245.     (void)dolaunch(&ls);
  246. }